<?php
// Load the Qcodo Development Framework
require('../includes/prepend.inc.php');
include_once('Panel/pnlHeader.php');
/**
 * This is a quick-and-dirty draft QForm object to do the List All functionality
 * of the Peopledetails class.  It uses the code-generated
 * PeopledetailsDataGrid control which has meta-methods to help with
 * easily creating/defining Peopledetails columns.
 *
 * Any display customizations and presentation-tier logic can be implemented
 * here by overriding existing or implementing new methods, properties and variables.
 *
 * NOTE: This file is overwritten on any code regenerations.  If you want to make
 * permanent changes, it is STRONGLY RECOMMENDED to move both peopledetails_list.php AND
 * peopledetails_list.tpl.php out of this Form Drafts directory.
 *
 * @package My Application
 * @subpackage Drafts
 */
class PeopledetailsListForm extends QForm {
	// Local instance of the Meta DataGrid to list Peopledetailses
	protected $dtgPeopledetailses;
	protected $lnkGContacts;
	protected $lnkHome;
	protected $txtSearchTerm;
	protected $btnSearch;
	protected $pnlHeader;
	// Create QForm Event Handlers as Needed

	//		protected function Form_Exit() {}
	//		protected function Form_Load() {}
	//		protected function Form_PreRender() {}
	//		protected function Form_Validate() {}

	protected function Form_Run() {
		// Security check for ALLOW_REMOTE_ADMIN
		// To allow access REGARDLESS of ALLOW_REMOTE_ADMIN, simply remove the line below
		QApplication::CheckRemoteAdmin();
		QApplication::$LoadedPage = 'My Contacts';
	}

	protected function Form_Create()
	{
		QApplication::$LoadedPage = 'My Contacts';
		// Instantiate the Meta DataGrid
		$this->dtgPeopledetailses = new PeopledetailsDataGrid($this);

		// Style the DataGrid (if desired)
		$this->dtgPeopledetailses->CssClass = 'datagrid';
		$this->dtgPeopledetailses->AlternateRowStyle->CssClass = 'alternate';

		// Add Pagination (if desired)
		$this->dtgPeopledetailses->Paginator = new QPaginator($this->dtgPeopledetailses);
		$this->dtgPeopledetailses->ItemsPerPage = 20;

		// Use the MetaDataGrid functionality to add Columns for this datagrid

		// Create an Edit Column
		$strEditPageUrl = 'peopledetails_edit.php';
		$this->dtgPeopledetailses->MetaAddEditLinkColumn($strEditPageUrl, 'Edit', 'Edit');

		// Create the Other Columns (note that you can use strings for peopledetails's properties, or you
		// can traverse down QQN::peopledetails() to display fields that are down the hierarchy)
		$this->dtgPeopledetailses->MetaAddColumn('Id');
		$this->dtgPeopledetailses->MetaAddColumn('FullName');
		$this->dtgPeopledetailses->MetaAddColumn('Address');
		$this->dtgPeopledetailses->MetaAddColumn('Phone');
		$this->dtgPeopledetailses->MetaAddColumn('Email');
		$this->lnkGContacts_Create();
		$this->lnkHome_Create();
			
		$this->txtSearchTerm_Create();
		$this->btnSearch_Create();
		$this->pnlHeader_Create();
	}

	protected function txtSearchTerm_Create()
	{
		$this->txtSearchTerm = new QTextBox($this);
	}

	protected function btnSearch_Create()
	{
		$this->btnSearch = new QButton($this);
		$this->btnSearch->Text = 'Search';
		$this->btnSearch->AddAction(new QClickEvent(),new QServerAction('Contact_Search'));
	}
		
		
	protected function lnkGContacts_Create()
	{
		$this->lnkGContacts = new QLinkButton($this);
		$this->lnkGContacts->Text = "Import Contacts from WebMail";
		$this->lnkGContacts->AddAction(new QClickEvent(),new QServerAction('lnkGContacts_Click',$this->objDefaultWaitIcon));
	}

	protected function lnkHome_Create()
	{
		$this->lnkHome = new QImageButton($this);
		$this->lnkHome->ImageUrl = '../assets/images/32x32/home.png';
		$this->lnkHome->AlternateText = 'Home';
		$this->lnkHome->ToolTip = 'Home';
		$this->lnkHome->AddAction(new QClickEvent(),new QServerAction('Home_Click'));
	}

	protected function lnkGContacts_Click()
	{
		QApplicationBase::Redirect('ImportContacts.php');
	}

	protected function Home_Click()
	{
		QApplicationBase::Redirect('index.php');
	}

	function Contact_Search()
	{
		$result = $this->GetContacts($this->txtSearchTerm->Text);
		$this->dtgPeopledetailses->DataSource = $result;
	}
	
	public static function GetContacts($strKeyword = null)
	{
		if(!is_null($strKeyword))
		{
			$condition = QQ::AndCondition(
															QQ::OrCondition(
															QQ::Like(QQN::Peopledetails()->FullName,'%'. $strKeyword . '%'),
															QQ::Like(QQN::Peopledetails()->Email,'%'. $strKeyword . '%'),
															QQ::Like(QQN::Peopledetails()->Address,'%'. $strKeyword . '%'),
															QQ::Like(QQN::Peopledetails()->Phone,'%'. $strKeyword . '%')
															),
															QQ::Equal(QQN::Peopledetails()->Owner,$_SESSION['User'])	
														   );
			$objPeopleArray = Peopledetails::QueryArray($condition);
			return $objPeopleArray;
		}
		else
		{
			$condition = QQ::Equal(QQN::Peopledetails()->Owner,$_SESSION['User']);
			$objPeopleArray = Peopledetails::QueryArray($condition);
			return $objPeopleArray;
		}
	}

	protected function pnlHeader_Create()
	{
		$this->pnlHeader = new pnlHeader($this, 'CloseEditPane');
	}
	
}

// Go ahead and run this form object to generate the page and event handlers, implicitly using
// peopledetails_list.tpl.php as the included HTML template file
PeopledetailsListForm::Run('PeopledetailsListForm');
?>